<template>
  <DataList 
  :cate="cate" 
  :cols="cols" 
  skipExcelNumber="3"
  :name="name"
  :excelHeader="excelHeader" 
 />
</template>
<script>
import DataList from "@/components/DataList";
import { getUser } from "@/utils/auth";
import { get开设专业,update开设专业,delete开设专业,add开设专业 } from '@/api/开设专业.js'
import linq from "linq";
import {findComponent} from "@/utils/ui";
import { addXwjd, deleteXwjd, updateXwjd, getXwjd,bulkAddXwjd } from "@/api/校外实习实训基地.js";
export default {
  components: {
    DataList
  },
  data() {
    return {
      cate: "A4-2校外实习实训基地表",
      name:"4.2 校外实习实训基地",
      excelHeader: {B3:"基地名称(全称)",C3:"依托单位",C4:"单位名称",D4:"在岗职工总数",E3:"建立日期（年月）",F3:"面向专业",F4:"总数（个）",G4:"主要专业",H3:"实习实训项目",H4:"总数（个）",I4:"主要项目(全称)",J3:"接待学生量(人次)",K3:"基地使用时间（天）",L3:"其中接受半年顶岗实习学生数(人)",M3:"是否有住宿条件",N3:"基地是否发放学生实习补贴",O3:"学校向基地支付专项实习经费（元/生）",P3:"学校派指导教师/学生管理人员（人次）",Q3:"接收应届毕业生就业数（人）"},
      cols: [
        { label: "基地名称(全称)", prop: "XWSXJDMC" },
        { label: "依托单位", prop: "校外实习实训基地_依托单位",
        data:[
            { label: "单位名称", prop: "YTDW"},
            { label: "在岗职工总数", prop: "ZGZGZS"},
        ] },
        { label: "建立日期（年月）", prop: "XWSXJDJLRQ" },
        { label: "面向专业", prop: "校外实习实训基地_面向专业",
        data:[
            { label: "总数(个)", prop: "MXZY_ZS"},
            { label: "主要专业", prop: "MXZY_ZYZY"},
        ] },
        { label: "实习实训项目", prop: "校外实习实训基地_实习实训项目",
        data:[
            { label: "总数(个)", prop: "SXSXXMZS"},
            { label: "主要项目(全称)", prop: "SXXM"},
        ] },
        { label: "接待学生量(人次)", prop: "JDXSL" },
        { label: "基地使用时间(天)", prop: "JDSYSJ" },
        { label: "其中接受半年顶岗实习学生数(人)", prop: "JSBNDGSXXSS" },
        { label: "是否有住宿条件", prop: "XZ_SFYZSTJ" },
        { label: "基地是否发放学生实习补贴", prop: "XZ_SFFFBT" },
        { label: "学校向基地支付专项实习经费(元/生)", prop: "XXXJDZFZXSXJF" },
        { label: "学校派指导教师/学生管理人员(人次)", prop: "XXPZDJSXSGLRRRC" },
        { label: "接收应届毕业生就业数(人)", prop: "JSYJBYSJYS" },
        
      ]
    };
  },
  beforeCreate(){
      
      
    },
  methods: {
    getSummaries(list){
      //
        const sums = [];
         this.cols.forEach((column, index) => {
            sums[index]="";
        });
        sums[0]="合计"
         sums[6]= "基地数:"+linq.from(list).distinct("$.XWSXJDMC").count();
          sums[7]= "单位数:"+linq.from(list).distinct("$.YTDW").count();
         sums[8]= "职工数:"+linq.from(list).sum("$.ZGZGZS");
          sums[10] = "专业数:"+linq.from(list).sum("$.MXZY_ZS");
          sums[12] = "项目数:"+linq.from(list).sum("$.SXSXXMZS");
          sums[14] = "接待学生:"+linq.from(list).sum("$.JDXSL");
          sums[15] = "使用时间:"+linq.from(list).sum("$.JDSYSJ");
          sums[16] = "顶岗实习:"+linq.from(list).sum("$.JSBNDGSXXSS");
          sums[17] = "有住宿:"+linq.from(list).where("$.XZ_SFYZSTJ=='是'").count();
          sums[18] = "发放补贴:"+linq.from(list).where("$.XZ_SFFFBT=='是'").count();
          sums[19] = "实习经费:"+linq.from(list).sum("$.XXXJDZFZXSXJF").toFixed(2);
          sums[20] = "管理人员:"+linq.from(list).sum("$.XXPZDJSXSGLRRRC");
          sums[21] = "就业数:"+linq.from(list).sum("$.JSYJBYSJYS");
          sums[22]= "通过:"+linq.from(list).where("$.审核状态=='通过'").count();
            sums[22] += ",不通过:"+linq.from(list).where("$.审核状态=='不通过'").count();
            sums[22] += ",待审核:"+linq.from(list).where("$.审核状态=='待审核'").count();
       return sums;
      //
    },
     async onFetchData(list) {
      list.forEach(e=>{
        e.MXZY_ZYZY=e.MXZY_ZYZY.split("、");
      })
     },
    async onChange(model,field,value){
      console.log(model,field,value,1111)
      if(field=='MXZY_ZYZY'){
        model.MXZY_ZS = value.length+''
      }
    },
    
    async onDlgCreated(){
      
      // var 专业 = await get开设专业(this.$root.user.XXID,this.$root.user.CJPC);
       var dd= findComponent(this, "MXZY_ZYZY");
      var data = linq.from(this.$root.开设专业).select("{...$,value:$.ZYMC+'('+$.ZYDM+')'}").distinct("$.value").toArray();
      dd.$attrs.items = data;
    },
    async fetchData() {
      var user = getUser();
      
      var datas = await getXwjd(user.XXID,user.CJPC);
      datas.forEach(e=>{
        e.MXZY_ZYZY=e.MXZY_ZYZY.split("、");
      })
      return datas;
    },
     add_check(model,list){
      //
      
          //是否已经存在数据
          var first =  linq.from(list).firstOrDefault(e=>e.XWSXJDMC ==  model.XWSXJDMC);
          if(first)
          return  "已经存在该基地名称"
      //
    },
    onadd(model){
      
      var temp = JSON.parse(JSON.stringify(model))
      temp.MXZY_ZYZY = temp.MXZY_ZYZY.join("、")
      return new Promise(async (resolve,reject)=>{
         try{
         
          console.log(JSON.stringify(temp));
          var model = await addXwjd(temp);
          model.MXZY_ZYZY = model.MXZY_ZYZY.split("、")
          resolve(model);
          }catch(e){
        reject(e);
      }
      })
    },

    async check(array,list){
      return new Promise(async (resolve,reject)=>{
        var user = getUser();
        var 专业 = await get开设专业(this.$root.user.XXID,this.$root.user.CJPC);
   
      array.forEach((e,i)=>{
          array[i].MXZY_ZYZY = array[i].MXZY_ZYZY.replace(/（/g, "(").replace(/）/g, ")")
        });
        //验证专业组合
        var err =  linq.from(array).selectMany("$.MXZY_ZYZY.split('、')").except(linq.from(专业).select("$.ZYMC+'('+$.ZYDM+')'").toArray()).toArray();
        linq.from(array).where(e=>linq.from(e.MXZY_ZYZY.split('、')).intersect(err).toArray().length>1).forEach(e=>e.error=e.error?e.error+";":''+"国家专业代码表不存在对应的专业组合");
        //
      
        var err =  linq.from(array).select("$.XWSXJDMC").intersect(linq.from(list).select("$.XWSXJDMC").toArray()).toArray();
          linq.from(array).where(e=>err.indexOf(e.XWSXJDMC) !==-1).forEach(e=>e.error=e.error?e.error+";":''+"数据库中已经存在该实验基地名称");
          
       resolve();
      });
    },
    async  bulkAdd(array){
       await  bulkAddXwjd({Xwjd:array});
    },
    onupdate(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))
      
      temp.MXZY_ZYZY = temp.MXZY_ZYZY.join("、")
      return new Promise(async (resolve,reject)=>{
          console.log(JSON.stringify(temp));
          var model = await updateXwjd(temp);
          resolve(model);
      })
      //
    },
    ondelete(model) {
       var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
        
         await deleteXwjd(temp);
          resolve();
      })
    }
  }
};
</script>
<style>
</style>